﻿using Microsoft.AspNetCore.Mvc;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Api
{
    [Route("Api/[controller]/[action]")]
    [Verify]
    public class UserGroupController : Base
    {

        private readonly UserGroupService adminUserGroupService;

        public UserGroupController( UserGroupService adminUserGroup)
        {
            adminUserGroupService = adminUserGroup;
        }
        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_Index)]
        public async Task<IActionResult> IndexAsync(int page = 1, int limit = 15)
        {
            var list = await adminUserGroupService.GetPageListAsync(page, limit);
            return Json(new { status = 1, data = list.Items, count = list.TotalItems });
        }
        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEditView)]
        public async Task<IActionResult> AddOrEditAsync(int id)
        {
            if (id > 0)
            {
                var data = await adminUserGroupService.GetNoCacheAsync(id);
                EditUserGroup viewUserGroup = new()
                {
                    Id = data.Id,
                    Groupname = data.Groupname,
                    Systemrole = data.Systemrole
                };
                return Json(new { status = 1, data = viewUserGroup });
            }
            else
            {
                return Json(new { status = 0, msg="参数错误！" });
            }
            

        }

        [HttpPost, ActionName("Create")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEdit)]
        [Log(Name = "新增角色")]
        public async Task<IActionResult> CreatePostAsync([FromBody]EditUserGroup adminUserGroup)
        {
            if (string.IsNullOrEmpty(adminUserGroup.Groupname))
                return Json(new { status = 0, msg = "角色名不能为空！" });

            if (await adminUserGroupService.GetByGroupNameAsync(adminUserGroup.Groupname) == false)
                return Json(new { status = 0, msg = "该角色名已存在，请重新输入！" });

            UserGroup admin = new()
            {
                Groupname = adminUserGroup.Groupname,
                Systemrole = adminUserGroup.Systemrole,
            };
            var res = await adminUserGroupService.InsertAsync(admin);
            return Json(new { status = 1, msg = "添加角色 ID：" + res.ToString() });
        }


        /// <summary>
        /// 修改
        /// </summary>
        [HttpPut, ActionName("Edit")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEdit)]
        [Log(Name = "修改角色")]
        public async Task<IActionResult> EditPost([FromBody] EditUserGroup adminUserGroup)
        {
            if (ModelState.IsValid)
            {
                var get = await adminUserGroupService.GetNoCacheAsync(adminUserGroup.Id);
                if (get == null)
                    return Json(new { status = 0, msg = "请勿非法提交！" });

                if (string.IsNullOrEmpty(adminUserGroup.Groupname))
                    return Json(new { status = 0, msg = "角色名不能为空！" });

                if (adminUserGroup.Groupname != get.Groupname)
                    if (await adminUserGroupService.GetByGroupNameAsync(adminUserGroup.Groupname) == false)
                        return Json(new { status = 0, msg = "该角色名已存在，请重新输入！" });

                get.Groupname = adminUserGroup.Groupname;
                get.Systemrole = adminUserGroup.Systemrole;

                await adminUserGroupService.UpdateAsync(get, adminUserGroup.Id);
                return Json(new { status = 1, msg = "修改角色及系统权限 ID：" + adminUserGroup.Id + adminUserGroup.Groupname });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpDelete]
        [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_Delete)]
        [Log(Name = "删除角色")]
        public async Task<IActionResult> Delete(int id)
        {
            if (await adminUserGroupService.DeleteByPriKeyAsync(id))
            {
                return Json(new { status = 1, msg = "删除角色 ID：" + id });
            }
            else
                return Json(new { status = 0, msg = "删除失败！可能该角色下分配有用户" });
        }

    }
}
